package com.sudichina.sudichina.view.activity.dentification;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.google.gson.reflect.TypeToken;
import com.sudichina.sudichina.Contains;
import com.sudichina.sudichina.MyApp;
import com.sudichina.sudichina.NET.Api;
import com.sudichina.sudichina.NET.NetUtils;
import com.sudichina.sudichina.NET.onRequestDataFinish;
import com.sudichina.sudichina.bean.Car;
import com.sudichina.sudichina.bean.Result;
import com.sudichina.sudichina.utils.DialogFrameAnimUtils;
import com.sudichina.sudichina.utils.Toastutils;
import com.sudichina.sudichina.view.activity.login.LoginActivity;
import com.sudichina.sudichina.view.activity.main.BaseActivity;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class CarManagerActivity extends BaseActivity implements onRequestDataFinish {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_car_manager);
        LoginActivity.startMeAndInitIsLogin(this, Contains.CARMANAGER_EXTRA);
        if (MyApp.getCurrentUser() == null) {
            return;
        }
        checkConfrimCar();


    }


    private void checkConfrimCar() {
//认证成功 就把信息展示出来
        Type type = new TypeToken<Result<List<Car>>>() {
        }.getType();
        NetUtils.requestData(Api.CARLISTBYSTATUS + MyApp.getCurrentUser().getId(), null, this, type, true);


    }

    @Override
    public void requestdataFinish(String catalog, Object bean) {


        if (catalog.equals(Api.CARLISTBYSTATUS + MyApp.getCurrentUser().getId())) {
            // TODO: 2017/3/17 0017
            //查询状态
            Result<ArrayList<Car>> bean1 = (Result<ArrayList<Car>>) bean;
            if (bean1.getCode() == 1) {
//请求成功 有审核通过车辆
                Intent intent = new Intent(this, CarSelectActivity.class);
                ArrayList<Car> carList = bean1.getData();

                intent.putParcelableArrayListExtra("carList", carList);

                intent.putExtra("carList", carList);
                startActivity(intent);

                finish();

            } else if (bean1.getCode() == 8015) {
                //无审核通过车辆
                //显示认证的界面
                startActivity(new Intent(CarManagerActivity.this, CarIdentificationActivity.class));
                finish();

            }
//根据返回的状态显示界面
//如果有认证的车辆  直接显示车辆界面
//
//            else显示认证的界面


        }

        if (catalog.equals(Api.CARRENZHENG)) {
            //提交的认证信息返回数据
//            Toastutils.showToast("提交成功");
            // TODO: 2017/3/17 0017

            Toastutils.showToast("提交成功"+bean.toString());

            DialogFrameAnimUtils.startSucessAnim(getApplicationContext(), this, "提交成功");
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    startActivity(new Intent(CarManagerActivity.this, CarSelectActivity.class));
                    finish();
                }
            }, 2000);


        }


    }


    @Override
    public void requestdataFailed(IOException e) {

    }
}
